const express = require("express");
const router = express.Router();
const db = require("../db"); // 引入数据库连接

// 宿舍管理员登录接口
router.post("/admin/login", (req, res) => {
    const { username, password } = req.body;

    if (!username || !password) {
        return res.status(400).json({ success: false, message: "账号或密码不能为空" });
    }

    const sql = "SELECT * FROM admin WHERE username = ? AND password = ?";
    db.query(sql, [username, password], (err, results) => {
        if (err) {
            return res.status(500).json({ success: false, message: "服务器内部错误" });
        }

        if (results.length === 0) {
            return res.status(401).json({ success: false, message: "账号或密码错误" });
        }

        res.json({ success: true, message: "登录成功" });
    });
});

module.exports = router;
